const mysql = require('mysql')

var Crawler = require("crawler");

var c = new Crawler({
    maxConnections: 10,
    // This will be called for each crawled page
    callback: function (error, res, done) {
        if (error) {
            console.log(error);
        } else {
            var $ = res.$;
            // $ is Cheerio by default
            //a lean implementation of core jQuery designed specifically for the server
            // console.log(res);
            JSON.parse(res.body).forEach(v => {

                // let heroUrl = `https://pvp.qq.com/web201605/herodetail/${v.ename}.shtml`
                // console.log(heroUrl);
                let heroUrl = `https://pvp.qq.com/web201605/herodetail/${v.ename}.shtml`
                xianqing.queue(heroUrl)

            });
        }
        done();
    }
});

// Queue just one URL, with default callback
c.queue('https://pvp.qq.com/web201605/js/herolist.json');

//进一步爬取详细信息
var xianqing = new Crawler({

    //最大连接数
    maxConnections: 10,
    // This will be called for each crawled page
    callback: function (error, res, done) {
        if (error) {
            console.log(error);
        } else {
            var $ = res.$;

            // $ is Cheerio by default
            //a lean implementation of core jQuery designed specifically for the server
            // console.log(res);
            //遍历拼接出每个英雄的地址
            // console.log($('h2.cover-name').text(), $('p.skill-name>b').eq(0).text(), $('a.hero-video img').attr('src'));

            //
            connection.query(`insert into hero(name,skill,icon) values('${$('h2.cover-name').text()}','${$('p.skill-name>b').eq(0).text()}','${$('a.hero-video img').attr('src')}')`, function (error, results, fields) {
                if (error) throw error;
                // console.log('The solution is: ', results[0].solution);
            });

        }
        done();
    }
});

//入库-----------------------

//创建一个连接
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'mymanager'
});